import pandas as pd


def mode_selection():
    print('Please use the following commands to select the desired mode:')
    print('\t1 - Splitting CSV \n',
          '\t2 - Merging XLSX_SHEET \n',
          '\telse - Exit')


print('Welcome to the csv2xlsx Processing!')
mode_selection()

flag = input('Your flag:\n')
flag = int(flag) if flag.isdigit() else 0

if flag == 1:
    data_path = input('Splitting CSV Mode--Enter file corresponding to "input.csv": \n').strip()
    rename_file_name = input('Rename merged XLSX like "output.xlsx":\n').strip()
    print('Processing..........')

    data = pd.read_csv(data_path)
    max_len = 1000000
    num_sheets = len(data) // max_len + 1
    writer = pd.ExcelWriter(data_path.replace('.csv', '.xlsx'), engine='xlsxwriter')

    for i in range(num_sheets):
        start, end = max_len * i, max_len * (i + 1)
        data.iloc[start:end, :].to_excel(writer, sheet_name=f'Sheet{i}', index=False)

    writer.save()
    print('Successfully saved to \n %s' % (data_path.replace('.csv', '.xlsx')))

elif flag == 2:
    data_path = input('Merging XLSX_SHEET Mode--Enter file corresponding to "input.xlsx": \n').strip()
    rename_file_name = input('Rename merged CSV like "output.csv":\n').strip()
    print('Processing..........')

    xls = pd.ExcelFile(data_path)
    sheet_names = xls.sheet_names
    data = pd.read_excel(xls, sheet_name=xls.sheet_names[0])

    if len(sheet_names) > 1:
        for sheet in sheet_names[1:]:
            data = pd.concat([data, pd.read_excel(xls, sheet_name=sheet)], axis=0)

    data.to_csv(rename_file_name, index=False)
    print('Successfully saved to \n %s' % (rename_file_name))

else:
    pass
_ = input()
